<?php


namespace App\backend\services;


use App\backend\models\GoodsGroupModel;
use App\backend\models\GoodsModel;
use App\Exceptions\InvalidRequestException;

class GoodsGroupServices
{

    /**
     * 获取 - 商品分组列表
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function getList($data)
    {
        $page = !isset($data['page']) ? 1 : $data['page'];unset($data['page']);
        $limit = !isset($data['limit']) ? 10 : $data['limit'];unset($data['limit']);

        $where_data = [];
        if (isset($data['name']) && !empty($data['name'])) {
            $where_data[] = ['name','like',"%{$data['name']}%"];
        }

        $list = GoodsGroupModel::listed($page, $limit, $where_data);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }

        foreach ($list as $value) {
            if (is_array($value)) {
                foreach ($value as $item) {
                    $where_data = ['group' => $item->id];
                    $item->goods_num = GoodsModel::goodsNumData($where_data);
                }
            }
        }

        return $list;
    }

    /**
     * 添加/编辑 - 商品分组详情
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function goodsGroupModify($data)
    {
        if (empty($data) && $data == false) {
            throw new InvalidRequestException(10009);
        }

        $id = isset($data['group_id']) && !empty($data['group_id']) ? $data['group_id'] : 0;unset($data['group_id']);

        $where_data = [];
        if ($id) {
            $data['updated_at'] = time();
            $where_data[] = ['id', '=', $id];
            GoodsGroupModel::updatedData($where_data, $data);
        } else {
            $data['created_at'] = time();
            $insert_id = GoodsGroupModel::insertGetIdData($data);
            if ($insert_id) {
                $where_data[] = ['id', '=', $insert_id];
            }
        }

        if (!empty($where_data)) {
            throw new InvalidRequestException(30007);
        }

        $select_data = ['id', 'name', 'desc', 'display_order'];
        $list = GoodsGroupModel::info($where_data, $select_data);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }

        return $list;
    }

    /**
     * 删除/下架 - 商品分组
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function updateGoodsGroupStatus($data)
    {
        if (empty($data) && $data == false) {
            throw new InvalidRequestException(10009);
        }

        $data['id'] = isset($data['group_id']) && !empty($data['group_id']) ? $data['group_id'] : 0;unset($data['group_id']);
        $update_data['status'] = !empty($data['status']) ? $data['status'] : 0;unset($data['status']);

        $list = GoodsGroupModel::deleteds($data, $update_data);
        if (empty($list)) {
            throw new InvalidRequestException(10009);
        }

        return $list;
    }

    /**
     * 详情 - 商品分组
     *
     * @param $data //请求参数数组
     * @return array
     */
    public static function goodsGroupInfo($data)
    {
        if (empty($data) && $data == false) {
            return [];
        }

        $select_data = ['id', 'name', 'desc', 'display_order', 'created_at'];
        $data['id'] = isset($data['group_id']) && !empty($data['group_id']) ? $data['group_id'] : 0;unset($data['group_id']);
        $list = GoodsGroupModel::info($data, $select_data);
        return $list;
    }

}
